package cn.manyufun.myf.ui.news.model;

import org.json.JSONException;
import org.json.JSONObject;

import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.bean.NewsSummaryList;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.commonutils.APIUtils;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.ui.news.contract.NewsListContract;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;

/**
 * @author Kevin Xiao
 * @date Create Date: 2017-12-27
 * @Description 新闻列表model
 */
public class NewsListModel implements NewsListContract.Model {
    /**
     * 获取新闻列表
     *
     * @param type
     * @param id
     * @param startPage
     * @return
     */
    @Override
    public Observable<NewsSummaryList> getNewsListData(final String type, final String id, final int startPage) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ImageSize", DisplayUtil.dip2px(80f) + "*" + DisplayUtil.dip2px(108f));
            root.put("LastPullTime", System.currentTimeMillis());
            root.put("PageIndex", startPage);
            root.put("PageSize", "20");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        return Api.getDefault(HostType.manyufun).getNewsList(requestBody)
                .compose(RxSchedulers.<NewsSummaryList>io_main());


//        return Api.getDefault(HostType.manyufun).getNewsList(Api.getCacheControl(), "aaa", "bbb", type, id, startPage)
//                .flatMap(new Func1<Map<String, List<NewsSummarys>>, Observable<NewsSummarys>>() {
//                    @Override
//                    public Observable<NewsSummarys> call(Map<String, List<NewsSummarys>> map) {
//                        if (id.endsWith(ApiConstants.HOUSE_ID)) {
//                            // 房产实际上针对地区的它的id与返回key不同
//                            return Observable.from(map.get("北京"));
//                        }
//                        return Observable.from(map.get(id));
//                    }
//                })
//                //转化时间
//                .map(new Func1<NewsSummarys, NewsSummarys>() {
//                    @Override
//                    public NewsSummarys call(NewsSummarys newsSummary) {
//                        String ptime = TimeUtil.formatDate(newsSummary.getPtime());
//                        newsSummary.setPtime(ptime);
//                        return newsSummary;
//                    }
//                })
//                .distinct()//去重
//                .toSortedList(new Func2<NewsSummarys, NewsSummarys, Integer>() {
//                    @Override
//                    public Integer call(NewsSummarys newsSummary, NewsSummarys newsSummary2) {
//                        return newsSummary2.getPtime().compareTo(newsSummary.getPtime());
//                    }
//                })
//                //声明线程调度
//                .compose(RxSchedulers.<List<NewsSummarys>>io_main());
    }
}
